Blog

Curt Gratz

March 02, 2010

Spread the word


Share your thoughts

I was working more with ColdFusion 9 ORM and found another small caveat I thought I would share.  When working with a relationship that is filtered, the implicit "has[property]", the has does not take into account the filter.  The implicit get works as expected however. 

UPDATE:  This is only an issue with a lazy loaded property.  Thanks Ben Nadel for pointing it out.

 

Consider the following persistent cfc...

<cfcomponent hint="I am the Album cfc" output="false" persistent="true" table="Albums">

<!--- CFC ID --->

<cfproperty name="AlbumID" type="numeric" fieldtype="id" />

<!--- Relationships --->

<cfproperty name="Photos" lazy="true" type="array" singularname="Photo" fkcolumn="AlbumID" orderby="Ind" fieldtype="one-to-many" cfc="Photo" inverse="false" where="DisplayPhoto='true'" />

<!--- Properties --->

<cfproperty name="AlbumName" type="string" />

<cfproperty name="AlbumCover" type="string" />

<cfproperty name="Ind" type="numeric" />

<!--- Functions --->

</cfcomponent>

First, notice that I have the cfc divided into four sections, the CFC ID, any relationships, any other properties of the cfc, and finally any additional functions.  I feel this is a handy way to organize your ORM cfc's.  I find that I need to refer to the ID and the relationships often, so I put them towards the top. 

OK, but onto the issue we are talking about.  Notice the relationship to that this CFC has to Photos is filtered with a where attribute of DisplayPhoto='true'.  This will return only the Photos that have the DisplayPhoto flag set to true and works perfect when we call the implicit getPhotos() method.  However, the hasPhoto() method does not take the where clause into account.  So, if we have an Album with lots of photos where they are all have the DisplayPhoto flag set to false, the implicit method would still return true.  To workaround this issue I wrote my own has function.

<cffunction name="hasPhoto" access="public" returntype="boolean" output="false">

<cfset var qry = queryNew("blah") />

<cfset var has = false />

 

<cfquery name="qry">

select count(PhotoID) as count from Photos where AlbumID = <cfqueryparam value="#this.getAlbumID()#">

and DiplayPhoto = 'true'

</cfquery>

<cfif qry.count GT 0>

<cfset has = true />

</cfif>

<cfreturn has />

</cffunction>

Add Your Comment

(5)

Mar 02, 2010 08:16:57 UTC

by Raymond Camden

So what is this "Coldfusion" you speak of? I only know of "ColdFusion". ;) Seriously though - darn good catch there!

Mar 02, 2010 08:39:33 UTC

by Ben Nadel

Does this still hold if the relationship is not lazy loaded?

Mar 02, 2010 09:15:50 UTC

by Curt Gratz

@Ray - Sorry, I perfer Coldfusion ;) @Ben - Excellent question. I ran a quick test and it does it works as expected when the relationship is not lazy loaded as it then just checks the same query as it used in the get. I will update the post to state such.

Mar 02, 2010 09:24:53 UTC

by Ben Nadel

@Curt, No problem - as someone who had not really played around with ORM much, I tend to ask really random questions :)

May 06, 2016 09:12:33 UTC

by Luis Majano

Great post Curt. You can also do a calculated field, which can improve performance: ``` <cfproperty name="numPhotos" formulat="select count(*) from Photos where AlbumID=AlbumID and DisplayPhoto=true"> ``` This will be optimized for you and always available

Recent Entries

12 Days of BoxLang - Day 1: ColdBox

12 Days of BoxLang - Day 1: ColdBox

ColdBox + BoxLang: The Future of Modern MVC on the JVM Welcome to Day 1 of the 12 Days of BoxLang

To kick off the series, we’re starting with one of the most powerful combinations in the Ortus ecosystem: ColdBox + BoxLang.

ColdBox has been the standard for modern CFML MVC development for over a decade. BoxLang is the next-generation dynamic language built for JVM and beyond. Together, they reshape how developers build web apps, APIs, microservices, CLIs, and soon desktop applications.

Let’s dive into why ColdBox 8 + BoxLang PRIME is a major milestone for the future of modern application development.

Maria Jose Herrera
Maria Jose Herrera
December 10, 2025
 Introducing CBWIRE v5.0!

 Introducing CBWIRE v5.0!

We are thrilled to announce the release of CBWIRE v5.0, the most powerful, stable, and developer-friendly version of CBWIRE ever shipped.

This major upgrade introduces deep BoxLang support, upgraded Livewire v3.6.4 features, enhanced security, improved error handling, performance gains, and long-requested developer experience improvements across the board.

Whether you're building full applications, dashboards, or reactive components inside ColdBox, CBWIRE v5.0 gives you more power with less friction.

Maria Jose Herrera
Maria Jose Herrera
December 10, 2025
Close the Year Strong: Secure Your 2026 CFML Consulting plan or Professional Support at a Special Rate

Close the Year Strong: Secure Your 2026 CFML Consulting plan or Professional Support at a Special Rate

As we approach the end of the year, many engineering teams face the same challenge: unused budget that must be spent before December 31 or a new 2026 budget that should be allocated strategically from the start.

If your organization relies on ColdFusion or Lucee, this is the ideal moment to secure expert support and ensure a stable, high-performing foundation for next year.

To help te...

Cristobal Escobar
Cristobal Escobar
December 10, 2025